﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eis51.CampanyCMS.Business.BaseInfoMng;
using Eis51.CampanyCMS.Model.Bus.QueryO;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Business.Bus;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.FrameWork;

namespace Eis51.CampanyCMS.WebUI.Admin.Order
{
    public partial class waitpaymoney : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
                btnSearch_Click(null, null);
            }
        }

        private void InitData()
        {
            Dict.Tag.BindListControl(rbtOrderStatus, typeof(OrderStatusType).Name);
            if (Request.QueryString["orderstatus"] != null)
            {
                rbtOrderStatus.SelectedValue = Request.QueryString["orderstatus"];
            }

        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            OrderApplyQuery query = new OrderApplyQuery();
            query.BeginDate = txtBeginDate.Text;
            query.EndDate = txtEndDate.Text;
            query.OrderNo = txtOderNo.Text;
            query.OrderStatus = rbtOrderStatus.SelectedValue;
            query.Seller = "dy001";// AppContext.CurrentAdministrator.UserName;
            this.dgOderList.Data = OrderApplyBLL.GetInstance().SearchSellerOrderList(query);
            this.dgOderList.DataBind();
        }

        protected void dgOderList_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            OrderApplyBLL bll = OrderApplyBLL.GetInstance();
            switch (e.CommandName)
            {
                case "Delete":
                    OrderApply o=new OrderApply() ;
                    o.OrderApplyId=int.Parse(e.Item.Cells[0].Text ) ;
                    bll.Delete(o);
                    this.btnSearch_Click(null, null);
                    break;
                case "View":
                    string url = UrlUtility.AddOrReplaceParam("vieworderDetail.aspx", "OrderSeqno", e.Item.Cells[0].Text.ToString(), true);
                    Response.Redirect(url);
                    break;
                case "ComfirmPay":
                    OrderApply ojb = OrderApplyBLL.GetInstance().Get(new OrderApply.Key(int.Parse(e.Item.Cells[0].Text)));
                    if (ojb.OrderStatus != (int)OrderStatusType.ConfirmRecevied)
                    {
                        ojb.OrderStatus = ojb.OrderStatus +1;
                    }
                    bll.Update(ojb);
                    this.btnSearch_Click(null, null);
                    break;
                default:
                    break;
            }
        }
        private string  ConfirmTitle(string id)
        {
            OrderApply ojb = OrderApplyBLL.GetInstance().Get(new OrderApply.Key(int.Parse(id)));
            if (ojb.OrderStatus == (int)OrderStatusType.WatiPay)
            {
                return "确认付款";
            }
            if (ojb.OrderStatus == (int)OrderStatusType.Payed)
            {
                return "确认已发货";
            }
            if (ojb.OrderStatus == (int)OrderStatusType.Sended)
            {
                return "确认交易完成";
            }
            if (ojb.OrderStatus == (int)OrderStatusType.ConfirmRecevied)
            {
                return "交易完毕";
            }
            return "";
        }

        protected void dgOderList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                Literal lblOrderStatus = e.Item.FindControl("lblOrderStatus") as Literal;
                lblOrderStatus.Text = Dict.Tag[(OrderStatusType)int.Parse(dv["Order_Status"].ToString())];
                HyperLink lnkNo = e.Item.FindControl("lnkNo") as HyperLink;
                string url = UrlUtility.AddOrReplaceParam("vieworderDetail.aspx", "OrderSeqno", dv["Order_Apply_ID"].ToString(), true);

                lnkNo.NavigateUrl = url;

                LinkButton btnComfirmPay = e.Item.FindControl("btnComfirmPay") as LinkButton;
                btnComfirmPay.Text = ConfirmTitle(dv["Order_Apply_ID"].ToString());

            }
        }
    }
}
